package cn.edu.cug.cs.gtl.oi.ui.actions;

import cn.edu.cug.cs.gtl.oi.ui.MainApp;
import cn.edu.cug.cs.gtl.oi.ui.projecttree.ProjectTree;
import cn.edu.cug.cs.gtl.oi.ui.projecttree.ProjectTreeNodeData;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Map;
import cn.edu.cug.cs.gtl.protos.Project;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class ProjectTreeSelectionListener implements TreeSelectionListener {
    MainApp theApp=null;

    public ProjectTreeSelectionListener(MainApp theApp) {
        this.theApp = theApp;
    }

    @Override
    public void valueChanged(TreeSelectionEvent e) {

        TreePath treePath = e.getPath();
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
        ProjectTreeNodeData userObject = (ProjectTreeNodeData) treeNode.getUserObject();
        Object refObject  =  userObject.getRefObject();
        if(refObject instanceof Project){
            //Do Nothing
        }
        else if(refObject instanceof Map){
            this.theApp.setCurrentMap((Map) refObject);
        }
        else if(refObject instanceof Layer){
            //Table Panel切换显示当前图层的数据表
            this.theApp.setCurrentLayer((Layer) refObject);
            this.theApp.getMainWindow().getTablePanel().reset();
        }
        else{

        }
    }
}
